package com.shelpe.services.sinbad.entity.jpa;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import com.shelpe.services.sinbad.entity.jpa.MobileKeywordsDetail.MobilePK;
import com.shelpe.services.sinbad.model.Keyword;

import lombok.Data;

/**
 * @description 养分关键词详情
 * 
 * @author letian@shelpe.com
 * @date 2017年8月28日 下午5:42:23
 */
@Entity(name = "mobile_keywords_detail")
@Data
@IdClass(MobilePK.class)
public class MobileKeywordsDetail {
		
	@Column(name="campaign_id")
	private long campaignID;
	
	@Column(name="adgroup_id")
	private  long adgroupID;
	
	@Id
	@Column(name="mobile_adgroup_id")
	private  long mobileAdgroupID;
	
	@Id
	@Column(name="keyword_id")
	private  long keywordID;
	
	@Column(name="word")
	private String word;
	
	@Column(name="start_qscore")
	private int startQscore;
	
	@Column(name="end_qscore")
	private Integer endQscore;
	
	@Convert(converter=StatusConverter.class)
	private Status status;
	
	//切成养分时的移动端价格
	@Column(name="initial_price")
	private int initialPrice;
	
	private String error;
	
	@Column(name="created_time")
	private Date createdTime;
	
	@Data
	public static class MobilePK implements Serializable {
		private static final long serialVersionUID = 1L;
		@Column(name = "mobile_adgroup_id")
		private long mobileAdgroupID;
		@Column(name = "keyword_id")
		private long keywordID;
	}
	
	public static enum Status{
		starting(1), success(2), error(3), exception(4);
		private int value;

		private Status(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.starting.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.starting;
			for (Status s : Status.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
	public void from(Keyword keyword,long mobileAdgroupID){
		this.campaignID = keyword.getCampaignId();
		this.adgroupID = keyword.getAdgroupId();
		this.keywordID = keyword.getKeywordId();
		this.word = keyword.getWord();
		this.startQscore = keyword.getWirelessQscore();
		this.endQscore = keyword.getWirelessQscore();
		this.status = Status.starting;
		this.mobileAdgroupID = mobileAdgroupID;
		this.initialPrice = keyword.getMaxMobilePrice();
		this.createdTime = new Date();
	}
}
